package com.gxa.springboot229.service.impl;

import com.gxa.springboot229.excption.BusinessException;
import com.gxa.springboot229.mapper.UserMapper;
import com.gxa.springboot229.pojo.User;
import com.gxa.springboot229.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper usermapper;

    /**
     * 通过电话查询
     * @param phone
     * @return
     */
    @Override
    public Integer findByPhone (String phone) {
        User user = new User();
        user.setPhone(phone);
        return usermapper.selectCount(user);
    }

    /**
     * 注册
     * @param usr
     * @return
     */
    @Override
    public Integer reg (User usr) {
        return usermapper.insertSelective(usr);
    }

    @Override
    public Map findMydata (int id) {
        return null;
    }

    @Override
    public Map findLogin (String phone) {
        User user = new User();
        Map<String,Object> map = new HashMap <>();
        user.setPhone(phone);
        User user1 = usermapper.selectOne(user);
        map.put("id",user1.getId());
        map.put("name",user1.getName());
        map.put("phone",user1.getPhone());
        return map;
    }

    /**
     * 用户修改接口
     * @param user
     * @return
     */
    @Override
    public Map updateUser (User user) {
        User user1 = new User();
        int i = usermapper.updateByPrimaryKeySelective(user);
        if(i == 0){
            throw new BusinessException("修改失败");
        }
        user1.setId(user.getId());
        User user2 = usermapper.selectOne(user1);
        Map map = new HashMap();
        map.put("id",user2.getId());
        map.put("name",user2.getName());
        map.put("phone",user2.getPhone());
        return map;
    }

}
